﻿using System;
using System.Collections.Generic;
using System.Text;
using EnCor.Security;

namespace EnCor
{
    public class AuthenticationProviderCollection
    {
        private List<IAuthenticationProvider> _InnerList = new List<IAuthenticationProvider>();

        public AuthenticationProviderCollection(IList<IAuthenticationProvider> providers)
        {
            foreach (IAuthenticationProvider provider in providers)
            {
                Add(provider);
            }
        }

        public IAuthenticationProvider GetAuthenticationProvider(string credentialType, string tag)
        {
            List<IAuthenticationProvider> list = new List<IAuthenticationProvider>();
            foreach (var p in _InnerList)
            {
                if (string.Compare(credentialType, p.CredentialType, true) == 0
                    && CheckTag(p.Tag, tag))
                {
                    return p;
                }
            }
            
            return null;
        }

        private bool CheckTag(string tagOfProvider, string tagToAuthenticate)
        {
            if (string.IsNullOrEmpty(tagToAuthenticate))
            {
                return true;
            }
            return string.Compare(tagOfProvider, tagToAuthenticate, true) == 0;
        }

        internal void Add(IAuthenticationProvider provider)
        {
            _InnerList.Add(provider);
        }
    }
}
